package com.jetinno.file;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import java.io.File;

/**
 * Created by zhangyuncai on 2023/9/21.
 */
public class TxtFragment extends Fragment {
    private String TAG = "TxtFragment";

    private EditText et_text_content;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_txt, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        et_text_content = view.findViewById(R.id.et_text_content);

        new Thread() {
            @Override
            public void run() {
                String content = TextUtils.join("\n", FileTools.fileToLines(getFile()));
                runOnUiThread(() -> {
                    et_text_content.setText(content);
                });
            }
        }.start();

        et_text_content.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                MenuInflater menuInflater = mode.getMenuInflater();
                menuInflater.inflate(R.menu.customer_menu, menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return true;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                if (item.getItemId() == R.id.italic) {
                    showCommonCharDialog();
                    return false;
                }
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {

            }
        });

    }

    private void showCommonCharDialog() {
        new CommonCharDialog(getContext(), et_text_content).show();
    }


    private TextActivity getTextActivity() {
        return (TextActivity) getActivity();
    }

    private File getFile() {
        return getTextActivity().getFile();
    }

    private void runOnUiThread(Runnable runnable) {
        getTextActivity().runOnUiThread(runnable);
    }

    public void close() {
        Log.i(TAG, "Yes");

        String content = et_text_content.getText().toString();
        new Thread() {
            @Override
            public void run() {
                boolean success = FileTools.stringToFile(getFile(), content);
                runOnUiThread(() -> {
                    if (success) {
                        FileTools.showToast(getContext(), String.format("Save Success : %s", getFile().getAbsoluteFile()));
                    } else {
                        FileTools.showToast(getContext(), "Save Fail");
                    }
                    getTextActivity().finish();
                });
            }
        }.start();
    }

}
